//表单验证
$(function(){
	$('#saveUserBtn').click(function () {
		//获取验证器
		var name = $('#name').val();
		var weixin = $('#weixin').val();
		var weixinQrCodeImage = $('#weixinQrCodeImage').attr("src");
		var zhifubao = $('#zhifubao').val();
		var zhifubaoQrCodeImage = $('#zhifubaoQrCodeImage').attr("src");
		var email = $('#email').val();
		var qq = $('#qq').val();
		var sex = $('#sex').val();
		var idCard = $('#idCard').val();
		var address = $('#address').val();
		
		if(name == null || name == ""){
			layer.msg("请输入您的姓名或昵称!",{icon:2,time:2000});
			return;
		}
		if(name.length > 32){
			layer.msg("姓名不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(weixin == null || weixin == ""){
			layer.msg("请输入微信号以便客户可以及时联系您!",{icon:2,time:2000});
			return;
		}
		if(weixin.length > 32){
			layer.msg("微信号不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(weixinQrCodeImage == null || weixinQrCodeImage == ""){
			layer.msg("请上传微信二维码,客户可以更方便联系您!",{icon:2,time:2000});
			return;
		}
		if(weixinQrCodeImage.length > 512){
			layer.msg("微信二维码url不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(zhifubao == null || zhifubao == ""){
			layer.msg("请输入支付宝账号以便客户可以及时联系您!",{icon:2,time:2000});
			return;
		}
		if(zhifubao.length > 32){
			layer.msg("支付宝账号不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(zhifubaoQrCodeImage == null || zhifubaoQrCodeImage == ""){
			layer.msg("请上传支付宝二维码,客户可以更方便联系您!",{icon:2,time:2000});
			return;
		}
		if(zhifubaoQrCodeImage.length > 512){
			layer.msg("支付宝二维码url不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(email != null && email != ""){
			email = email.trim();
			if(!isEmail(email)){
				layer.msg("邮箱不符合规范!",{icon:2,time:2000});
				return;
			}
		}
		if(qq != null && qq.length > 12){
			layer.msg("qq号不能超过12位!",{icon:2,time:2000});
			return;
		}
		
		if(idCard != null && idCard.length > 20){
			layer.msg("身份证号不能超过20位!",{icon:2,time:2000});
			return;
		}
		
		if(address != null && address.length > 512){
			layer.msg("地址不能超过512位!",{icon:2,time:2000});
			return;
		}
		var user = {};
		user["name"] = name;
		user["weixin"] = weixin;
		user["weixinQrCode"] = weixinQrCodeImage;
		user["zhifubao"] = zhifubao;
		user["zhifubaoQrCode"] = zhifubaoQrCodeImage;
		user["qq"] = qq;
		user["sex"] = sex;
		user["idCard"] = idCard;
		user["address"] = address;
		$.ajax({
			url:baselocation+'/user-update',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(user),
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					window.location.href=baselocation+'/user-info';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
	//保存员工信息
	$('#saveEmployeeBtn').click(function () {
		var employeeId = $('#employeeId').val();
		//获取验证器
		var loginName = $('#loginName').val();
		var password = $('#password').val();
		var repassword = $('#repassword').val();
		var email = $('#email').val();
		var name = $('#name').val();
		var weixin = $('#weixin').val();
		var weixinQrCodeImage = $('#weixinQrCodeImage').attr("src");
		var zhifubao = $('#zhifubao').val();
		var zhifubaoQrCodeImage = $('#zhifubaoQrCodeImage').attr("src");
		var qq = $('#qq').val();
		var sex = $('#sex').val();
		var idCard = $('#idCard').val();
		var address = $('#address').val();
		
		var businessType = $('input[name="businessType"]:checked').val();
		
		var leaderId = $('#leaderId').val();
		var leaderName = $('#leaderName').val();
		
		if(loginName == null || loginName == ""){
			layer.msg("手机号不能为空!",{icon:2,time:2000});
			return;
		}
		if(!isPhoneNo(loginName)){
			layer.msg("请输入11位正常手机号!",{icon:2,time:2000});
			return;
		}
		if(name == null || name == ""){
			layer.msg("请输入员工姓名或昵称!",{icon:2,time:2000});
			return;
		}
		if(name.length > 32){
			layer.msg("姓名不能超过32位!",{icon:2,time:2000});
			return;
		}
		//只有新增时需要校验密码
		if(employeeId == null || employeeId == "" || employeeId == "0"){
			if(password == null || password == ""){
				layer.msg("密码不能为空!",{icon:2,time:2000});
				return;
			}
			if(!isCode(password)){
				layer.msg("亲,密码只能是英文字母和数字组合,不区分大小写哦!",{icon:2,time:2000});
				return;
			}
			if(password.length < 6){
				layer.msg("请输入至少6位密码!",{icon:2,time:2000});
				return;
			}
			if(repassword == null || repassword == ""){
				layer.msg("重复密码不能为空!",{icon:2,time:2000});
				return;
			}
			
			if(repassword != password){
				layer.msg("两次密码输入不一致!",{icon:2,time:2000});
				return;
			}
		}
		
		if(email != null && email != ""){
			email = email.trim();
			if(!isEmail(email)){
				layer.msg("邮箱不符合规范!",{icon:2,time:2000});
				return;
			}
		}
		if(weixin != null && weixin.length > 32){
			layer.msg("微信号不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(weixinQrCodeImage != null && weixinQrCodeImage.length > 512){
			layer.msg("微信二维码url不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(qq != null && qq.length > 12){
			layer.msg("qq号不能超过12位!",{icon:2,time:2000});
			return;
		}
		
		if(idCard != null && idCard.length > 20){
			layer.msg("身份证号不能超过20位!",{icon:2,time:2000});
			return;
		}
		
		if(address != null && address.length > 512){
			layer.msg("地址不能超过512位!",{icon:2,time:2000});
			return;
		}
		var employee = {};
		employee["id"] = employeeId;
		employee["loginName"] = loginName;
		employee["pwd"] = password;
		employee["repwd"] = repassword;
		employee["email"] = email;
		employee["mobile"] = loginName;
		employee["name"] = name;
		employee["weixin"] = weixin;
		employee["weixinQrCode"] = weixinQrCodeImage;
		employee["zhifubao"] = zhifubao;
		employee["zhifubaoQrCode"] = zhifubaoQrCodeImage;
		employee["qq"] = qq;
		employee["sex"] = sex;
		employee["idCard"] = idCard;
		employee["address"] = address;
		
		employee["type"] = businessType;
		
		employee["leaderId"] = leaderId;
		employee["leaderName"] = leaderName;
		
		$.ajax({
			url:baselocation+'/employee',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(employee),
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					window.location.href=baselocation+'/employee';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
	
	$('#leaderName').focus(function () {
		//禁止弹出手机键盘
		document.activeElement.blur();
		$('#employeeListDiv').attr("style","display:block;");//显示div
	})
	
	$('#hiddenListBtn').click(function () {
		$('#employeeListDiv').attr("style","display:none;");//隐藏div
	})
//	$('#hiddenListBtn').on('touchstart',function(){
//		$('#employeeListDiv').attr("style","display:none;");//隐藏div
//		//马上让它不能点击
//		$('#employeeReturn').css('pointer-events','none');
//		//因为click事件需要300ms响应，所以我们时间定义360ms,时间一过又可以正常点击了
//		setTimeout(function(){$('#employeeReturn').css('pointer-events','auto')},360);
//	});
	$('#clearLeaderBnt').click(function () {
		$('#leaderId').val('');
		$('#leaderName').val('');
	})
	$("input[name='businessType']").click(function(){
		var t = $(this).val();
		//如果是经理，需要隐藏上级经理的输入,并且清空leader的值
		if(t == 2){
			$('#leaderDiv').attr("style","display:none;");//隐藏div
			$('#leaderId').val('');
			$('#leaderName').val('');
		}else{
			//禁止弹出手机键盘
			document.activeElement.blur();
			$('#leaderDiv').attr("style","display:block;");//显示div
		}
	});
	var businessType = $('input[name="businessType"]:checked').val();
	if(businessType == 2){
		$('#leaderDiv').attr("style","display:none;");//隐藏div
	}else{
		//禁止弹出手机键盘
		document.activeElement.blur();
		$('#leaderDiv').attr("style","display:block;");//显示div
	}
});
function selectMyChoice(id,name){
	$('#leaderId').val(id);
	$('#leaderName').val(name);
	$('#employeeListDiv').attr("style","display:none;");//隐藏div
}